{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8db9e970",
   "metadata": {},
   "source": [
    "# 网络参数\n",
    "在这一节里，我们学习网络参数相关内容。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bb9bc4b",
   "metadata": {},
   "source": [
    "## 一、数据类型\n",
    "daata type简称dtype，MindSpore张量支持不同的数据类型dtype，如int8、int16、int32、int64、float16、float32、float64等，其与NumPy数据类型一一对应。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7a11cc03",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Int32\n"
     ]
    }
   ],
   "source": [
    "import mindspore as ms\n",
    "print(ms.int32)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "696cd6d8",
   "metadata": {},
   "source": [
    "### 不同数据类型间相互转换\n",
    "MindSpore提供了支持不同类型相互转换的接口，如下：\n",
    "- `dtype_to_nptype`：将MindSpore的数据类型转换为NumPy对应的数据类型。\n",
    "- `dtype_to_pytype`：将MindSpore的数据类型转换为Python对应的内置数据类型。\n",
    "- `pytype_to_dtype`：将Python内置的数据类型转换为MindSpore对应的数据类型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2452f88b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Float32\n",
      "<class 'numpy.float32'>\n",
      "<class 'float'>\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "dtype = ms.float32\n",
    "print(dtype)\n",
    "dtype = ms.dtype_to_nptype(ms.float32)\n",
    "print(dtype)\n",
    "dtype = ms.dtype_to_pytype(ms.float32)\n",
    "print(dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a003402b",
   "metadata": {},
   "source": [
    "## 二、网络变量\n",
    "在网络训练过程中可以改变的参数称之为网络参数。常见的有权重weight和偏置bias。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6567ba3d",
   "metadata": {},
   "source": [
    "### 变量初始化\n",
    "我们可以使用mindspore中的`Parameter`接口对变量进行初始化，参数如下：\n",
    "- `default_input`：为输入数据，支持多种数据类型；\n",
    "- `name`：可设置变量的名称，用于在网络中区别于其他变量；\n",
    "- `requires_grad`：表示在网络训练过程，是否需要计算参数梯度。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "68fa1493",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parameter (name=w, shape=(), dtype=Float32, requires_grad=True) 3.0\n",
      "Parameter (name=b, shape=(), dtype=Float32, requires_grad=True) 2.0\n",
      "Parameter (name=a, shape=(1, 3), dtype=Float32, requires_grad=True) [[1. 2. 3.]]\n"
     ]
    }
   ],
   "source": [
    "w = ms.Parameter(default_input=3.0, name='w', requires_grad=True)\n",
    "b = ms.Parameter(default_input=2.0, name='b', requires_grad=True)\n",
    "\n",
    "tensor = ms.Tensor(np.array([[1, 2, 3]]), ms.float32)\n",
    "a = ms.Parameter(default_input=tensor, name='a')\n",
    "\n",
    "print(w, w.asnumpy())\n",
    "print(b, b.asnumpy())\n",
    "print(a, a.asnumpy())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e226b13",
   "metadata": {},
   "source": [
    "更多的，我们会使用`Initializer`来创造Parameter："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "a8354960",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parameter (name=x, shape=(1, 2, 3), dtype=Float32, requires_grad=True)\n"
     ]
    }
   ],
   "source": [
    "from mindspore.common.initializer import initializer\n",
    "\n",
    "x = ms.Parameter(default_input=initializer('ones', [1, 2, 3], ms.float32), name='x')\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6da56f53",
   "metadata": {},
   "source": [
    "其实就是用初始化器Initializer来初始化Tensor。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6cc483ec",
   "metadata": {},
   "source": [
    "### 变量操作\n",
    "我们可以对网络变量之间可以进行一些常规操作："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc03cd89",
   "metadata": {},
   "source": [
    "1. `clone`：克隆变量张量`Parameter`，克隆完成后可以给新的变量`Parameter`指定新的名称。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "1e0c08ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parameter (name=new x, shape=(1, 2, 3), dtype=Float32, requires_grad=True)\n"
     ]
    }
   ],
   "source": [
    "x_clone = x.clone()\n",
    "x_clone.name = \"new x\"\n",
    "print(x_clone)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68c447d0",
   "metadata": {},
   "source": [
    "2. `set_data`：修改变量`Parameter`的数据或形状`shape`。\n",
    "\n",
    "其中，`set_data`方法有`data`和`slice_shape`两种入参。`data`表示变量`Parameter`新传入的数据；`slice_shape`表示是否修改变量`Parameter`的形状`shape`，默认为False。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "695adf71",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parameter (name=x, shape=(1, 2), dtype=Float32, requires_grad=True) [[1. 1.]]\n",
      "Parameter (name=x, shape=(1, 2), dtype=Float32, requires_grad=True) [[0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "print(x, x.asnumpy())\n",
    "new_x = x.set_data(ms.Tensor(np.zeros((1, 2)), ms.float32), slice_shape=True)\n",
    "print(new_x, new_x.asnumpy())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mindspore",
   "language": "python",
   "name": "mindvision"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
